package com.micro.modual.extension;

import com.micro.common.MicroConstant;
import com.micro.common.biz.GeneratorCodeUtil;
import com.micro.common.biz.TreeUtil;
import com.micro.common.util.ObjectUtil;
import com.micro.common.util.SnowFlakeUtil;
import com.micro.modual.entity.GoodsCategory;
import com.micro.modual.model.bo.goodscategory.GoodsCategoryBO;
import com.micro.modual.model.tree.CategoryTreeDTO;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * <h1>商品分类表 延展类</h1>
 *
 * @author LevelCoder
 * @version V1.0.0
 * @email LevelCoder@126.com
 * @date 2020-08-04 00:23:27
 */
public class GoodsCategoryExtension {

    /**
     * <h2>转换: 商品分类表 新增</h2>
     *
     * @param goodsCategoryBO {@link GoodsCategoryBO} 商品分类表 新增/修改 请求对象
     * @return {@link GoodsCategory} 商品分类表
     */
    public static GoodsCategory converAddGoodsCategory(GoodsCategoryBO goodsCategoryBO) {
        String defaultIcon = "http://image.90hyscxy.com/eyeglass/20200720/2e8e6fe561f34fc98863bb1bdec34275.jpeg";
        GoodsCategory goodsCategory = new GoodsCategory();
        goodsCategory.setId(SnowFlakeUtil.getId());
        goodsCategory.setCategoryCode(GeneratorCodeUtil.generatorSystemCode());
        goodsCategory.setCategoryIcon(ObjectUtil.isNotEmpty(goodsCategoryBO.getCategoryIcon())? goodsCategoryBO.getCategoryIcon():defaultIcon);
        goodsCategory.setCategoryLevel(goodsCategoryBO.getCategoryLevel());
        goodsCategory.setCategoryName(goodsCategoryBO.getCategoryName());
        goodsCategory.setCategoryParentCode(goodsCategoryBO.getCategoryParentCode());
        goodsCategory.setCategorySort(goodsCategoryBO.getCategorySort());
        goodsCategory.setCategoryStatus(goodsCategoryBO.getCategoryStatus());
        goodsCategory.setTemplateCode(goodsCategoryBO.getTemplateCode());
        goodsCategory.setCreateTime(new Date());
        return goodsCategory;
    }

    /**
     * <h2>转换: 商品分类表 修改</h2>
     *
     * @param goodsCategoryBO {@link GoodsCategoryBO} 商品分类表 新增/修改 请求对象
     * @return {@link GoodsCategory} 商品分类表
     */
    public static GoodsCategory converEditGoodsCategory(GoodsCategoryBO goodsCategoryBO) {
        GoodsCategory goodsCategory = new GoodsCategory();
        goodsCategory.setId(goodsCategoryBO.getId());
        goodsCategory.setCategoryName(goodsCategoryBO.getCategoryName());
        goodsCategory.setCategoryParentCode(goodsCategoryBO.getCategoryParentCode());
        goodsCategory.setCategoryIcon(goodsCategoryBO.getCategoryIcon());
        goodsCategory.setCategoryLevel(goodsCategoryBO.getCategoryLevel());
        goodsCategory.setCategorySort(goodsCategoryBO.getCategorySort());
        goodsCategory.setCategoryStatus(goodsCategoryBO.getCategoryStatus());
        goodsCategory.setUpdateTime(new Date());
        goodsCategory.setCategoryCode(goodsCategoryBO.getCategoryCode());
        goodsCategory.setTemplateCode(goodsCategoryBO.getTemplateCode());
        return goodsCategory;
    }

    /**
     * <h2>构造商品分类树</h2>
     * @param categoryList
     * @return
     */
    public static List<CategoryTreeDTO> buildCategoryTree(List<GoodsCategory> categoryList) {
        List<CategoryTreeDTO> categoryTreeList = new ArrayList<CategoryTreeDTO>();
        for (GoodsCategory category : categoryList) {
            CategoryTreeDTO categoryTree = new CategoryTreeDTO();
            categoryTree.setCategoryName(category.getCategoryName());
            categoryTree.setCode(category.getCategoryCode());
            categoryTree.setParentCode(category.getCategoryParentCode());
            categoryTreeList.add(categoryTree);
        }
        return TreeUtil.bulid(categoryTreeList, MicroConstant.SystemDefaultValue.DEFAULT_PARENT_CODE);
    }

}
